package com.tronsis.distribution.activity.me.coupons;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageAndVideoPreviewSeeActivity;
import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.CouponBean;
import com.tronsis.distribution.biz.factory.OrderBizFactory;
import com.tronsis.distribution.http.Url;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DateUtil;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.lzy.imagepicker.ImagePicker.REQUEST_CODE_PREVIEW;

public class CouponsActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_coupons)
    RecyclerView mRvCoupons;

    private CommonAdapter<CouponBean> mCouponsAdapter;
    private List<CouponBean> mCouponsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupons);
        ButterKnife.bind(this);
        setTitle(true, "优惠券");

        initRv();
    }

    @Override
    protected void onResume() {
        super.onResume();
        httpGetCouponsList();
    }

    private void initRv() {
        mCouponsAdapter = new CommonAdapter<CouponBean>(mContext, mCouponsList, R.layout.item_coupons) {
            @Override
            protected void convert(ViewHolder holder, final CouponBean couponBean, int position) {
                holder.setText(R.id.tv_expired_on, getString(R.string.format_expired_on, DateUtil.getDateTimeString(couponBean.getExpiredOn())));

                holder.setImageByUrl(mContext, R.id.iv_cover, Url.FILE_DOWNLOAD + couponBean.getCoverFid());

                boolean used = couponBean.isUsed();
                long currentTime = DateUtil.getServerCurrentTime(mContext);
                long expired = DateUtil.getStringToDate(couponBean.getExpiredOn()).getTime();
                if (used||currentTime > expired) {
                    holder.setVisible(R.id.iv_useless, true);
                    holder.setVisible(R.id.btn_use_coupon, false);
                } else {
                    holder.setVisible(R.id.iv_useless, false);
                    holder.setVisible(R.id.btn_use_coupon, true);
                }

                holder.setOnClickListener(R.id.iv_cover, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ArrayList<ImageItem> mImageList = new ArrayList<>();
                        ImageItem imageItem = new ImageItem(Url.FILE_DOWNLOAD + couponBean.getCoverFid(),System.currentTimeMillis());
                        mImageList.add(imageItem);

                        Intent intentPreview = new Intent(mContext, ImageAndVideoPreviewSeeActivity.class);
                        intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, 0);
                        intentPreview.putParcelableArrayListExtra(ImagePicker.EXTRA_IMAGE_ITEMS, mImageList);
                        startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);
                    }
                });

                Button button = holder.getView(R.id.btn_use_coupon);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, CouponsUseActivity.class);
                        intent.putExtra("CouponBean", couponBean);
                        startActivity(intent);
                    }
                });
            }
        };

        int height = DisplayUtil.getInstance(mContext).dp2px(1);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvCoupons.addItemDecoration(decoration);
        mRvCoupons.setLayoutManager(new LinearLayoutManager(mContext));
        mRvCoupons.setAdapter(mCouponsAdapter);
    }

    private void httpGetCouponsList() {
        try {
            OrderBizFactory.createBiz().getCouponList(this, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    CouponBean couponBean = (CouponBean) data;
                    if (couponBean != null) {
                        mCouponsList.clear();
                        mCouponsList.add(couponBean);
                        mCouponsAdapter.notifyDataSetChanged();
                    }

                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

}
